unit frm_ChargeDogovor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Card, DBCtrls, DBCtrlsEh, StdCtrls, Mask, ActnList,
  ComCtrls, ToolWin, Buttons, ExtCtrls, FIBQuery, pFIBQuery, pFIBStoredProc,
  DB, DateUtils, FIBDataSet, pFIBDataSet, FIBDatabase, pFIBDatabase,
  PropFilerEh, PropStorageEh;

type
  TfrmChargeDogovor = class(TshblCard)
    aMeters: TAction;
    aCalc: TAction;
    GroupBox1: TGroupBox;
    DBText2: TDBText;
    Label2: TLabel;
    Label1: TLabel;
    DBText1: TDBText;
    dscServName: TDataSource;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    dsGenCharge: TpFIBDataSet;
    dscGenCharge: TDataSource;
    Panel2: TPanel;
    GroupBox2: TGroupBox;
    dbtChargeDate: TDBDateTimeEditEh;
    pan22: TGroupBox;
    Label13: TLabel;
    Label14: TLabel;
    dblcBMonth: TDBComboBoxEh;
    dbBYear: TDBNumberEditEh;
    dblcEMonth: TDBComboBoxEh;
    dbEYear: TDBNumberEditEh;
    dsServName: TpFIBDataSet;
    dsServNameSERVICE_NAME: TFIBStringField;
    dsServNameADDRESS: TFIBStringField;
    GroupBox3: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    lbItogo: TLabel;
    Label5: TLabel;
    DBText3: TDBText;
    DBText4: TDBText;
    DBText5: TDBText;
    DBText6: TDBText;
    Label6: TLabel;
    DBText7: TDBText;
    dsGenChargeS_BASE: TFIBBCDField;
    dsGenChargeS_EX: TFIBBCDField;
    dsGenChargeS_OUT: TFIBBCDField;
    dsGenChargeS_IN: TFIBBCDField;
    dsGenChargeSUMM: TFIBBCDField;
    dsServNameMETER_EXIST: TFIBSmallIntField;
    LocalTrans: TpFIBTransaction;
    dsServNameABONENT_ID: TFIBIntegerField;
    dsServNameGROUP_ID: TFIBIntegerField;
    ChargeTrans: TpFIBTransaction;
    procedure aMetersExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure aCalcExecute(Sender: TObject);
    procedure aCancelExecute(Sender: TObject);
    procedure aSaveExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dbBYearEditButtons0Click(Sender: TObject;
      var Handled: Boolean);
    procedure dbBYearEditButtons1Click(Sender: TObject;
      var Handled: Boolean);
    procedure dbEYearEditButtons0Click(Sender: TObject;
      var Handled: Boolean);
    procedure dbEYearEditButtons1Click(Sender: TObject;
      var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
    Dogovor_id:integer;
  end;

var
  frmChargeDogovor: TfrmChargeDogovor;

function ChargeDogovorDlg(FDogovor_id :integer; aOwner :TComponent) :boolean;

implementation

uses frm_ChargeMeter, uDM;

{$R *.dfm}

function ChargeDogovorDlg(FDogovor_id :integer; aOwner :TComponent) :boolean;
begin
  Result := false;
  
  with TfrmChargeDogovor.Create(aOwner) do begin
    Dogovor_id := FDogovor_id;

    if ShowModal = mrOk then Result := true;

    Free;
  end;

end;

procedure TfrmChargeDogovor.aMetersExecute(Sender: TObject);
begin
  frmChargeMeter:= TfrmChargeMeter.Create(Self);
  frmChargeMeter.Dogovor_id := Dogovor_id;
  frmChargeMeter.ShowModal;
  frmChargeMeter.Free;
end;

procedure TfrmChargeDogovor.FormCreate(Sender: TObject);
begin
  inherited;
  Dogovor_id := 0;
end;

procedure TfrmChargeDogovor.aCalcExecute(Sender: TObject);
begin
  if not Dogovor_id > 0 then exit;
  dsGenCharge.Close;
  if dsGenCharge.Transaction.Active then
    dsGenCharge.Transaction.Rollback;
    
//  dsGenCharge.ParamByName('DOGOVOR_ID').Value := Dogovor_id;
  dsGenCharge.ParamByName('ABONENT_ID').Value := dsServNameABONENT_ID.Value;
  dsGenCharge.ParamByName('GROUP_ID').Value := dsServNameGROUP_ID.Value;
  dsGenCharge.ParamByName('PBDATE').Value := EncodeDate(dbBYear.Value, dblcBMonth.Value, 1);
  dsGenCharge.ParamByName('PEDATE').Value := EncodeDate(dbEYear.Value, dblcEMonth.Value, DaysInAMonth(dbEYear.Value, dblcEMonth.Value));
  dsGenCharge.ParamByName('DOCDATE').Value := dbtChargeDate.Value;

  if not dsGenCharge.Database.Connected then exit;

  dsGenCharge.Open;
end;

procedure TfrmChargeDogovor.aCancelExecute(Sender: TObject);
begin
  if dsGenCharge.Active then dsGenCharge.Close;
  dsGenCharge.Transaction.Rollback;
  ModalResult := mrCancel;

end;

procedure TfrmChargeDogovor.aSaveExecute(Sender: TObject);
begin
  if not dsGenCharge.Active then aCalcExecute(Sender);
  dsGenCharge.Transaction.Commit;
  ModalResult := mrOk;
end;

procedure TfrmChargeDogovor.FormShow(Sender: TObject);
var wday, wMonth, wYear: word;
begin
  dsServName.ParamByName('dogovor_id').Value := Dogovor_id;
  if not dsServName.Database.Connected then exit;

  dsServName.Open;

  BitBtn3.Visible := dsServNameMETER_EXIST.AsBoolean;

  DecodeDate(Now, wYear, wMonth, wday);
  if wday <= 10 then wMonth := wMonth - 1;

  if wMonth = 0 then
  begin
    wMonth := 12;
    wYear := wYear - 1;
  end;

  dblcBMonth.Value := wMonth;
  dbBYear.Value := wYear;
  dblcEMonth.Value := wMonth;
  dbEYear.Value := wYear;

  dbtChargeDate.Value := Now;
end;

procedure TfrmChargeDogovor.dbBYearEditButtons0Click(Sender: TObject;
  var Handled: Boolean);
begin
  dbBYear.Value := dbBYear.Value - 1;
end;

procedure TfrmChargeDogovor.dbBYearEditButtons1Click(Sender: TObject;
  var Handled: Boolean);
begin
  dbBYear.Value := dbBYear.Value + 1;
end;

procedure TfrmChargeDogovor.dbEYearEditButtons0Click(Sender: TObject;
  var Handled: Boolean);
begin
  dbEYear.Value := dbEYear.Value - 1;
end;

procedure TfrmChargeDogovor.dbEYearEditButtons1Click(Sender: TObject;
  var Handled: Boolean);
begin
  dbEYear.Value := dbEYear.Value + 1;
end;

end.
